home *** CD-ROM | disk | FTP | other *** search
- Path: news.mira.net.au!news
- From: davidw@werple.net.au (David White)
- Newsgroups: comp.lang.c++
- Subject: Re: type-casting in multi-inheritance
- Date: 12 Feb 1996 20:20:29 +1100
- Organization: Werple Internet, Melbourne
- Message-ID: <4fn0ot$msd@werple.net.au>
- References: <DMn7Ix.G9@Virginia.EDU>
- NNTP-Posting-Host: werple.mira.net.au
-
- gcl8a@Virginia.EDU (Gregory Carl Lewin) writes:
-
- >I am a bit fuzzy about casting from pointers to derived objects
- >to base classes when you are using multiple inheritance.
- >Perhaps someone could explain the following to me. Consider
- >the arrangement:
-
- >class Base1 {};
- >class Base2 {};
- >class Derived12 : public Base1, Base2 {};
-
- >Base1 b1;
- >Base2 b2;
- >Derived12 d12;
-
- >void foo(void) {
- >Base1* pb1;
- >pb1=&b1;
- >pb1=&d12;
-
- >Base2* pb2;
- >pb2=&b2;
- >pb2=&d12; //ERROR: Cannot convert pointer
- >pb2=(Base2*) &d12; //OK: But is it ALWAYS safe?
-
- >}
-
- >Because Derived 12 is publicly derived from Base1 and Base2, I
- >would have expected the conversion of &d12 to a Base2* to be
- >automatic; however, the comiler balks. The subsequent explicit
- >conversion works, and the program seems to act appropriately,
- >but using the explicit type-cast scares me. Can someone
- >explain why this is and if this is the "correct" way around the
- >problem (alternatives being virtual inheritance with class Base
- >being a base for Base1 and Base2, and ???).
-
- The problem is that Derived12 is _not_ publicly derived from Base2.
- You need:
- class Derived12 : public Base1, public Base2 {};
-
- David White
- davidw@werple.mira.net.au
-